export class ReadContext {
	stacks: any[] = [{}]

	// record stack keys
	private stackKeys: string[] = []

	public get current(): any {
		return this.stacks[this.stacks.length - 1]
	}

	public set current(value: any) {
		this.stacks[this.stacks.length - 1] = value
		if (this.stacks.length >= 2) {
			this.stacks[this.stacks.length - 2][this.currentKey] = value
		}
	}

	public get currentKey(): string {
		return this.stackKeys[this.stackKeys.length - 1]
	}

	public get currentLevel(): number {
		return this.stacks.length
	}

	public pop(toLength: number) {
		while (this.stacks.length > toLength) {
			this.stacks.pop()
			this.stackKeys.pop()
		}
	}

	public push(key: string, value: any = null): any {
		this.current[key] = value
		this.stacks.push(this.current[key])
		this.stackKeys.push(key)
	}

	public get root(): any {
		return this.stacks[0]
	}

	public setCurrentProperty(key: string, value: any) {
		let obj = this.current
		if (obj === null) {
			this.current = {}
			obj = this.current
		}
		obj[key] = value
	}
}
